package cn.edu.gdu.quizpractice.dao;

import cn.edu.gdu.quizpractice.entity.ChoiceQuestion;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.*;
import org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

@Mapper
public interface ChoiceQuestionDao extends BaseMapper<ChoiceQuestion>{
    @Select("select * from t_choicequestions where category_id = #{categoryId}")
    List<ChoiceQuestion> findByCategory_Id(Integer categoryId);

    @Select("select * from t_choicequestions where category_name = #{categoryName}")
    List<ChoiceQuestion> findByCategoryName(String name);

    @Select("select * from t_choicequestions where difficulty = #{difficulty}")
    List<ChoiceQuestion> findByDifficulty(Integer difficulty);

    @Select("select * from t_choicequestions")
    List<ChoiceQuestion> findAll();

    @Insert("insert into t_choicequestions(b_name, category_name, cq_content, option_a, option_b, option_c, " +
            "option_d, correct_answer, analysis, difficulty, is_multiple) values(#{bName}, #{categoryName}, " +
            "#{cqContent}, #{optionA}, #{optionB}, #{optionC}, #{optionD}, #{correctAnswer}, #{analysis}, " +
            "#{difficulty}, #{isMultiple})")
    int insert(ChoiceQuestion choiceQuestion);

    /**
     * 自定义批量插入方法（XML 实现）
     */
    int insertBatch(@Param("cachedList") List<ChoiceQuestion> cachedList);

    @Update("update t_choicequestions set b_name = #{bName}, category_name = #{categoryName},"+
    "cq_content = #{cqContent}, option_a = #{optionA}, option_b = #{optionB}, option_c = #{optionC},"+
    "option_d = #{optionD}, correct_answer = #{correctAnswer}, analysis = #{analysis}, "+
    "difficulty = #{difficulty}, is_multiple = #{isMultiple} where cquestion_id = #{cquestionId}")
    boolean updateQuestion(ChoiceQuestion question);

    @Delete("DELETE FROM t_choicequestions WHERE b_name = (SELECT name FROM t_banks WHERE b_id = #{bankId})")
    boolean deleteByBankId(@Param("bankId") Integer bankId);

} 